package com.game.sdk.pay.alipay;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.game.sdk.YTAppService;
import com.game.sdk.domain.ParamJson;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.init.NetCallBack;
import com.game.sdk.util.Constants;
import com.game.sdk.util.DataSafeUtil;
import com.game.sdk.util.GetDataImpl;
import com.game.sdk.util.NetTask;
import com.game.sdk.util.Util;

public class NetUtil {
	private static final String TAG = "NetUtil";
	private static NetUtil getdataImpl;
	private static Context ctx;

	private NetUtil(Context ctxs) {
		this.ctx = ctxs;
	}

	public static NetUtil getInstance(Context ctxs) {
		if (null == getdataImpl) {
			getdataImpl = new NetUtil(ctxs);
		}
		if (ctx == null) {
			ctx = ctxs;
		}
		return getdataImpl;
	}


	/**
	 * 请求ali 
	 * 
	 * @param mem_id 
	 * @param user_token
	 * @param amount
	 * @param realAmount
	 * @param pay_token
	 * @param ctx
	 * @param callback
	 * @return
	 */
	public static void reqAlipay(String mem_id, String user_token,
			String amount,String realAmount, String pay_token, final Context ctx,
			final NetCallBack callback) {
		String jasonStr = "";
		jasonStr = getAlipayParamsObjs(mem_id, user_token, amount,realAmount, pay_token)
				.toString();
		new NetTask() {

			protected void onPostExecute(String result) {
				Util.netResponseCommHandler(1, result, callback);
			};
		}.execute(ctx, Constant.URL_CHARGER_ZIFUBAO, jasonStr, true, true,
				true, true);
	}

	/**
	 * alipay参数的获取
	 * 
	 * @param mem_id
	 * @param pay_token
	 * @param amount
	 * @param pay_token
	 * */
	private static Object getAlipayParamsObjs(String mem_id, String user_token,
			String amount, String realAmount,String pay_token) {
		ParamJson json = new ParamJson();
		json.setAppid(YTAppService.appid);

		json.setAmount(amount);
		
		json.setRealAmount(realAmount);

		json.setUserid(mem_id);

		json.setUser_token(user_token);

		json.setCode(DataSafeUtil.code + "");

		json.setFrom(ParamJson.ANDROID+"");
		
		json.setClient_id(YTAppService.clientId + "");

		String api_token = DataSafeUtil.getApiToken("alipay",
				System.currentTimeMillis(), YTAppService.clientKey);
		json.setApi_token(api_token);

		json.setPay_token(pay_token);
		json.setParams_key(DataSafeUtil.getIdentifyKey(json.getParamsString(),
				YTAppService.clientKey));

		return json.buildParams();

	}
}
